<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Birds Flying</title>
  <style>
    canvas {
      position: absolute;
      width: 500px;
      height: 500px;
    }
  </style>
</head>
<body>
  <div>sprites: <span id="spriteCount">1</span></div>
  <canvas id="bgcanvas" width="800" height="800"></canvas>
  <canvas id="fgcanvas" width="800" height="800"></canvas>
  <script src="/js/mesh.js"></script>
  <script>
    const fgcanvas = document.getElementById('fgcanvas');
    const bgcanvas = document.getElementById('bgcanvas');
    const bgcontext = bgcanvas.getContext('2d');
    const axisZero = [400, 400];
    function drawCircle() {
      bgcontext.fillStyle = 'rgba(17,51,153,0.5)';
      bgcontext.arc(...axisZero, 400, 0, 360, false);
      bgcontext.fill();
    }
    drawCircle();

    const wings = [[2, 126, 86, 60], [2, 64, 86, 60], [2, 2, 86, 60]];

    const textureURL = 'https://p.ssl.qhimg.com/d/inn/c886d09f/birds.png';
    (async function () {
      const {Renderer, Figure2D, Mesh2D} = meshjs;
      const renderer = new Renderer(fgcanvas, {
        contextType: 'webgl2',
      });

      const center = [400, 400];

      const figure = new Figure2D();
      figure.rect(0, 0, 86, 60);

      const meshList = [];

      const texture = await renderer.loadTexture(textureURL);

      function moveTo(bird, ang) {
        const {x: x0, y: y0} = bird;
        const x1 = 350 * Math.cos(ang);
        const y1 = 350 * Math.sin(ang);
        const distance = Math.sqrt((x1 - x0) ** 2 + (y1 - y0) ** 2);
        const startTime = Date.now(),
          T = 5 * distance + 100;

        requestAnimationFrame(function f() {
          let p = (Date.now() - startTime) / T;
          p = Math.min(1.0, p);
          const x = x0 + p * (x1 - x0);
          const y = y0 + p * (y1 - y0);
          bird.x = x;
          bird.y = y;
          bird.setTransform(1, 0, 0, 1, x, y);
          if(ang > 0.5 * Math.PI && ang < 1.5 * Math.PI) {
            bird.scale(-1, 1, [43, 30]);
          }
          if(p < 1) {
            requestAnimationFrame(f);
          } else {
            setTimeout(() => {
              const newAng = Math.random() * 2 * Math.PI;
              moveTo(bird, newAng);
            }, 500);
          }
        });
      }

      function addBird() {
        const bird = new Mesh2D(figure);
        // mesh.setFill({
        //   color: [1, 0, 0, 0.8],
        // });
        let i = 0;
        function setTexture(i) {
          bird.setTexture(texture, {
            scale: false,
            repeat: false,
            // rect: [0, 0, 86, 60],
            srcRect: wings[i % 3],
          });
        }
        setInterval(() => {
          setTexture(++i);
        }, 100);
        setTexture(i);
        meshList.push(bird);
        spriteCount.innerHTML = meshList.length;

        bird.x = 0;
        bird.y = 0;

        const ang = Math.random() * 2 * Math.PI;
        moveTo(bird, ang);
      }

      addBird();

      let birdCount = 1;
      const timer = setInterval(() => {
        if(birdCount++ < 1500) addBird();
        else clearInterval(timer);
      }, 0);

      renderer.setGlobalTransform(1, 0, 0, 1, center[0] - 43, center[1] - 30);
      function update() {
        // renderer.clear();
        // meshList.forEach((bird) => {
        //   bird.setTransform(1, 0, 0, 1, 0, 0);
        //   bird.translate(bird.x, bird.y);
        // });
        renderer.drawMeshes(meshList);
        requestAnimationFrame(update);
      }

      update(0);
    }());
  </script>
</body>
</html>